package com.yzy.thread;

import sun.misc.Unsafe;

import java.lang.reflect.Field;

public class MyUnsafe {

    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
        Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
        theUnsafe.setAccessible(true);
        Unsafe unsafe = (Unsafe) theUnsafe.get(null);
        System.out.println(unsafe);
        Long offset = unsafe.objectFieldOffset(User.class.getDeclaredField("name"));
        byte b = unsafe.getByteVolatile(User.class, offset - 4);
        byte c = unsafe.getByteVolatile(User.class, offset - 8);
        byte d = unsafe.getByteVolatile(User.class, offset - 12);
        System.out.println(b);
        System.out.println(c);
        System.out.println(d);
        //System.out.println(f);
        synchronized (User.class) {
            byte b1 = unsafe.getByteVolatile(User.class, offset - 4);
            byte c1 = unsafe.getByteVolatile(User.class, offset - 8);
            byte d1 = unsafe.getByteVolatile(User.class, offset - 12);
            System.out.println(b1);
            System.out.println(c1);
            System.out.println(d1);
        }
        /*Long offset1 = unsafe.objectFieldOffset(User.class.getDeclaredField("bool"));
        Long offset2 = unsafe.objectFieldOffset(User.class.getDeclaredField("aByte"));
        Long offset22 = unsafe.objectFieldOffset(User.class.getDeclaredField("aByte2"));
        Long offset3 = unsafe.objectFieldOffset(User.class.getDeclaredField("aChar"));
        Long offset4 = unsafe.objectFieldOffset(User.class.getDeclaredField("aShort"));
        Long offset5 = unsafe.objectFieldOffset(User.class.getDeclaredField("anInt"));
        Long offset6 = unsafe.objectFieldOffset(User.class.getDeclaredField("aFloat"));
        Long offset7 = unsafe.objectFieldOffset(User.class.getDeclaredField("aDouble"));
        Long offset8 = unsafe.objectFieldOffset(User.class.getDeclaredField("aLong"));
        Long offset9 = unsafe.objectFieldOffset(User.class.getDeclaredField("boolB"));
        Long offset10 = unsafe.objectFieldOffset(User.class.getDeclaredField("aByteB"));
        Long offset11 = unsafe.objectFieldOffset(User.class.getDeclaredField("aCharC"));
        Long offset12 = unsafe.objectFieldOffset(User.class.getDeclaredField("aShortS"));
        Long offset13 = unsafe.objectFieldOffset(User.class.getDeclaredField("anIntI"));
        Long offset14 = unsafe.objectFieldOffset(User.class.getDeclaredField("aFloatF"));
        Long offset15 = unsafe.objectFieldOffset(User.class.getDeclaredField("aDoubleD"));
        Long offset16 = unsafe.objectFieldOffset(User.class.getDeclaredField("aLongL"));
        System.out.println(offset);*/

    }

    static class User {

        public User() {}

        private String name;
/*
        private boolean bool;  //40

        private byte aByte; //41

        private byte aByte2;  //42

        private byte aByte3;  //43

        private byte aByte4;  //44

        private byte aByte5;

        private char aChar;

        private short aShort;

        private int anInt;

        private float aFloat;

        private double aDouble;

        private long aLong;

        private Boolean boolB;

        private Byte aByteB;

        private Character aCharC;

        private Short aShortS;

        private Integer anIntI;

        private Float aFloatF;

        private Double aDoubleD;

        private Long aLongL;*/

    }

}
